package com.clstu.houserent.houseservice;

import com.clstu.houserent.house.House;
import com.clstu.houserent.utility.Utility;

import java.util.Scanner;

public class HouseService {
    private House[] arr;
    private int count=0;
    private int sum;
    private Scanner myScanner=new Scanner(System.in);

    public HouseService(int sum){
        this.sum=sum;
        arr=new House[sum];
        arr[0]=new House(111,"jack","18844654655","南岸区",1000,"已出租");
    }

    public House[] list(){
        return arr;
    }

    public boolean add(){
        if(count==sum) {
            System.out.println("max，上限已到，无法添加");
            return false;
        }
        int index=0;
        House ne=newHouse();
        for (int i = 0; i < count; i++) {
            if(ne.getId()>arr[i].getId()){
                index=i;
            }
        }
        for(int i=++count;i>index;i--){
            arr[i]=arr[i-1];
        }
        arr[index]=ne;
        return true;
    }

    public House serch(){
        if(count==-1){
            System.out.println("当前目录为空，没有可删除项目");
            return null;
        }
        System.out.println("请输入要查找的房间的房间号");
        int id= Utility.myint();
        int index=findIndex(arr,id,0,count);
        if(index==-1){
            System.out.println("你输入的房间号不存在");
            return null;
        }
        return  arr[index];
    }

    public boolean change(){
        if(count==-1){
            System.out.println("当前目录为空，没有可修改项目");
            return false;
        }
        System.out.println("请输入要修改的房间的房间号");
        int id= Utility.myint();
        int index=findIndex(arr,id,0,count);
        if(index==-1){
            System.out.println("你输入的房间号不存在");
            return false;
        }
        House h=arr[index];
        System.out.println("原信息如下：");
        System.out.println("房号 \t房主 \t电话 \t地址 \t价格 \t状态");
        System.out.println(h);

        System.out.println("新房主");
        String name=myScanner.nextLine();
        if(!name.equals("")){
            h.setRenter(name);
        }

        System.out.println("新电话");
        String no=myScanner.nextLine();
        if(!no.equals("")){
            h.setPhone(no);
        }

        System.out.println("新地址");
        String add=myScanner.nextLine();
        if(!add.equals("")){
            h.setAdd(add);
        }

        System.out.println("新价格");
        h.setPrice(myScanner.nextDouble());

        System.out.println("新状态");
        String state=myScanner.nextLine();//读掉多余的换行
        state=myScanner.nextLine();
        if(!state.equals("")){
            h.setState(state);
        }

        return true;
    }

    public boolean del(){
        if(count==-1){
            System.out.println("当前目录为空，没有可删除项目");
            return false;
        }
        System.out.println("请输入要删除的房间的房间号");
        int id= Utility.myint();
        int index=findIndex(arr,id,0,count);
        if(index==-1){
            System.out.println("你输入的房间号不存在");
            return false;
        }
        for (int i = index; i <= count; i++) {
            arr[i]=arr[i+1];
        }
        count--;
        return  true;
    }

    public int findIndex(House[] arr,int id,int l,int r){//用二分法查找下标
        if(l==r&&arr[l].getId()!=id) return -1;
        int mid=(l+r)/2;
        if(arr[mid].getId()==id) return mid;
        if(arr[mid].getId()>id) return  findIndex(arr,id,l,mid);
        if(arr[mid].getId()<id) return findIndex(arr,id,mid+1,r);
        return -1;
    }

    public House newHouse(){
        System.out.println("请输入房号");
        int id=myScanner.nextInt();
        System.out.println("请输入房主姓名");
        String renter=myScanner.next();
        System.out.println("请输入电话号码");
        String no=myScanner.next();
        System.out.println("请输入地址");
        String add=myScanner.next();
        System.out.println("请输入价格");
        double price=myScanner.nextDouble();
        System.out.println("请输入状态");
        String state=myScanner.next();
        return new House(id,renter,no,add,price,state);
    }
}
